package cn.fl.settlement.controller;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.EncryptUtil;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.capital.constant.SettleConstant;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.settle.condition.PrjStlRfdRcdQC;
import cn.fl.settle.dto.PrjStlRfdRcdDTO;
import cn.fl.settlement.service.ISettlementService;
import cn.fl.settlement.vo.CapCstStmApplyVO;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* @Description:    结清退款管理Controller
* @Author:         liurong
* @CreateDate:     2020/3/26 16:53
* @Version:        1.0
*/
@Controller
@RequestMapping("/settlement/refund")
public class SettleRefundController extends BaseController {

    @Autowired
    private ISettlementService settlementService;

    @Autowired
    private ISysDictionaryApiService dictionaryApiService;

    /**
     * 到自动退款记录列表页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/toRefundListPage.do", method = {RequestMethod.GET})
    public ModelAndView toRefundListPage() {
        ModelAndView modelAndView = new ModelAndView("settlement/autoRefundList");
        // 退款记录状态
        List<KeyValueVO> stsCdList = this.dictionaryApiService.getKvList(SettleConstant.REFUND_STS_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());

        modelAndView.addObject("stsCdList", JSON.toJSONString(stsCdList));
        return modelAndView;
    }

    /**
     * 加载自动退款列表数据
     * @param request
     * @param condition
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/loadRefundList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object loadRefundList(HttpServletRequest request, PrjStlRfdRcdQC condition) {
        try {
            if (StringUtils.isNotEmpty(condition.getOrderId())) {
                condition.setId(Long.parseLong(condition.getOrderId()));
            }
            if (StringUtils.isNotEmpty(condition.getApplyIdStr())) {
                condition.setApplyId(Long.parseLong(condition.getApplyIdStr()));
            }
            // 按创建时间倒序
            condition.setSortName("CRT_TM");
            condition.setSortOrder("DESC");
            String currentPage = request.getParameter("page");
            String pageSize = request.getParameter("pagesize");

            Pager page = new Pager();
            if (StringUtils.isNotEmpty(currentPage)) {
                page.setCurrentPage(Integer.parseInt(currentPage));
            }
            if (StringUtils.isNotEmpty(pageSize)) {
                page.setEveryPage(Integer.parseInt(pageSize));
            }
            page.setCondition(condition);
            Pager pager = settlementService.searchAutoRefundListPage(page);
            ListPageVO<PrjStlRfdRcdDTO> listPage = new ListPageVO<>((List<PrjStlRfdRcdDTO>) pager.getResultList(), pager.getTotalCount());
            return listPage;
        } catch (Exception e) {
            BaseLogUtils.newLogger("加载自动退款列表数据失败！userId=" + CurrentThreadContext.getCurrentUserId() + ",userName=" + CurrentThreadContext.getCurrentUserName()).setThrowable(e).error(LogExceptionEnum.COMMON_CODE_0002);
            throw e;
        }
    }

    @RequestMapping(value = "/queryRefundDetail.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object queryRefundDetail(HttpServletRequest request) {
        try {
            String id = request.getParameter("orderId");

            PrjStlRfdRcdDTO dto = settlementService.selectAutoRefundById(Long.parseLong(id));
            if (dto != null) {
                // 脱敏处理
                dto.setActNo(EncryptUtil.cardNoEncrypt(dto.getActNo()));
                dto.setIdentityNo(EncryptUtil.certNoEncrypt(dto.getIdentityNo()));
                dto.setActTel(EncryptUtil.mobileEncrypt(dto.getActTel()));
            }
            List<PrjStlRfdRcdDTO> list = Lists.newArrayList();
            list.add(dto);
            ListPageVO<PrjStlRfdRcdDTO> listPage = new ListPageVO<>(list, list.size());
            return listPage;
        } catch (Exception e) {
            BaseLogUtils.newLogger("查询退款详情数据失败！userId=" + CurrentThreadContext.getCurrentUserId() + ",userName=" + CurrentThreadContext.getCurrentUserName()).setThrowable(e).error(LogExceptionEnum.COMMON_CODE_0002);
            throw e;
        }
    }


}

